﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace ConnectCsharpToMysql.objects
{
    class Video
    {
        public String idVideo;
        public String nameVideo;
        public String trailerVideo;
        public String desVideo;
        public String avatar;
        public String status;
        public String createDate;
        public String totalPart;
        public String idCategory;

        public Video() { }

        public Boolean select()
        {
            String strSelectQuery = "SELECT * FROM tbvideo WHERE idVideo = " + idVideo;
            DataTable dataTable = new DBConnect().Select(strSelectQuery);
            if (dataTable.Rows.Count > 0)
            {
                nameVideo = dataTable.Rows[0]["nameVideo"].ToString();
                trailerVideo = dataTable.Rows[0]["trailerVideo"].ToString();
                desVideo = dataTable.Rows[0]["desVideo"].ToString();
                avatar = dataTable.Rows[0]["avatar"].ToString();
                status = dataTable.Rows[0]["status"].ToString();
                createDate = dataTable.Rows[0]["createDate"].ToString();
                totalPart = dataTable.Rows[0]["totalPart"].ToString();
                idCategory = dataTable.Rows[0]["idCategory"].ToString();
                return true;
            }
            else
            {
                return false;
            }
        }

        public static List<Video> selectAll(String categoryId)
        {
            List<Video> listCategory = new List<Video>();
            String strSelectQuery = "SELECT * FROM tbvideo idCategory = " + categoryId;
            DataTable dataTable = new DBConnect().Select(strSelectQuery);
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                Video video = new Video();
                video.idVideo = dataTable.Rows[0]["idVideo"].ToString();
                video.nameVideo = dataTable.Rows[0]["nameVideo"].ToString();
                video.trailerVideo = dataTable.Rows[0]["trailerVideo"].ToString();
                video.desVideo = dataTable.Rows[0]["desVideo"].ToString();
                video.avatar = dataTable.Rows[0]["avatar"].ToString();
                video.status = dataTable.Rows[0]["status"].ToString();
                video.createDate = dataTable.Rows[0]["createDate"].ToString();
                video.totalPart = dataTable.Rows[0]["totalPart"].ToString();
                video.idCategory = dataTable.Rows[0]["idCategory"].ToString();
                listCategory.Add(video);
            }
            return listCategory;
        }

        public Boolean insert()
        {
            String strInserQuery = "INSERT INTO tbvideo(nameVideo,trailerVideo,desVideo,avatar,status,createDate,totalPart,idCategory) " +
                                    "VALUES(N'" + nameVideo + "','" + trailerVideo + "',N'" + desVideo + "','" + avatar + "'," + status + ",'" + createDate + "'," + totalPart + "," + idCategory + ")";
            return new DBConnect().Insert(strInserQuery);
        }

        public Boolean update()
        {
            String strUpdateQuery = "UPDATE tbvideo " +
                        "SET nameVideo=N'" + nameVideo + "',trailerVideo='" + trailerVideo + "',desVideo=N'" + desVideo + "',avatar='" + avatar + "',status='" + status + "',totalPart='" + totalPart + "',idCategory='" + idCategory + "' " +
                        "WHERE idVideo=" + idVideo;
            return new DBConnect().Update(strUpdateQuery);
        }

        public Boolean delete()
        {
            String strDeleteQuery = "DELETE FROM tbvideo WHERE idVideo = " + idVideo;
            return new DBConnect().Delete(strDeleteQuery);
        }

    }
}
